<?php
/**
 * Loads styles and scripts for the theme
 * Handles attachment files by displaying appropriate 'object'
 *
 * Other media is handled through the theme extensions
 * get-the-image.php, get-the-object.php
 *
 * @package one
 * @subpackage Media
 */

/**
 * Function to load CSS at an appropriate time
 * Adds print.css if users chooses to use it
 *
 * Users should load their own CSS using wp_enqueue_style()
 * @link http://codex.wordpress.org/Function_Reference/wp_enqueue_style
 *
 * @since 0.1
 */
function one_enqueue_style() {
	global $one_settings;

	if ( !$one_settings['print_style'] )
		return;

	wp_enqueue_style( 'one-print', one_CSS . '/print.css', false, '0.2', 'print' );
	wp_print_styles( array( 'one-print' ) );
}

/**
 * Function to load JavaScript at appropriate time
 * Loads pullquotes.js if users chooses
 * Loads comment reply script if users chooses nested comments
 * Loads flash-embed.js if on flv attachment page
 *
 * Users should load custom JavaScript with wp_enqueue_script()
 * @link http://codex.wordpress.org/Function_Reference/wp_enqueue_script
 *
 * As of 0.5, the comments popup script is no longer loaded
 * Users should add this through their child theme
 *
 * Loads Superfish and related scripts
 * @link http://users.tpg.com.au/j_birch/plugins/superfish
 * Users can customize their Superfish dropdowns
 * Uses apply_filters( 'one_drop_downs_script' )
 *
 * @since 0.1
 */
function one_enqueue_script() {
	global $one_settings;

	/*
	* jQuery pullquotes
	*/
	if ( $one_settings['pullquotes_js'] )
		wp_enqueue_script( 'one_pullquotes', one_JS . '/pullquotes.js', array( 'jquery' ), 0.5, true );

	/*
	* Comment reply (WP 2.7+)
	*/
	if ( is_singular() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );

	/*
	* Flash video embed
	*/
	if ( is_attachment() && get_post_mime_type() == 'application/octet-stream' ) :
		if ( preg_match( '/\.flv$/', wp_get_attachment_url() ) )
			wp_enqueue_script( 'flash-embed', one_JS . '/flash-embed.js', false, 0.1 );
	endif;

	/*
	* Superfish drop-down menus
	*/
	if ( $one_settings['hoverintent_js'] )
		wp_enqueue_script( 'hoverintent', one_JS . '/hoverIntent.js', array( 'jquery' ), 0.1, true );

	if ( $one_settings['superfish_js'] ) :
		wp_enqueue_script( 'superfish', one_JS . '/superfish.js', array( 'jquery' ), 1.4, true );
		wp_enqueue_script( 'supersubs', one_JS . '/supersubs.js', array( 'jquery' ), 0.2, true );

		$drop_downs = one_JS . '/drop-downs.js';
		$drop_downs = apply_filters( 'one_drop_downs_script', $drop_downs );
		wp_enqueue_script( 'one-drop-downs', $drop_downs, array( 'jquery' ), 0.1, true );
	endif;
}

/**
 * Loads the correct function for handling attachments
 * Checks the attachment mime type to call correct function
 * Videos are loaded with one_video_attachment()
 * Audio is loaded with one_audio_attachment()
 * Text is loaded with one_text_attachment()
 * Applications are loaded with one_application_attachment()
 *
 * Function uses get_post_mime_type() and wp_get_attachment_url()
 * @link http://codex.wordpress.org/Function_Reference/get_post_mime_type
 * @link http://codex.wordpress.org/Function_Reference/wp_get_attachment_url
 *
 * Image attachments are not loaded with this function
 * The functionality for them resides in image.php
 *
 * Ideally, all attachments would be appropriately handled within their templates
 * However, this could lead to messy template files
 * For now, we'll use separate functions for handling attachment content
 * The biggest issue here is with handling different video types
 *
 * @since 0.5
 */
function one_attachment() {

	$mime_type = get_post_mime_type();
	$file = wp_get_attachment_url();

	if ( preg_match( '/video/', $mime_type ) || preg_match( '/\.flv$/', $file ) )
		$attachment = one_video_attachment( $mime_type, $file );

	elseif ( preg_match( '/audio/', $mime_type ) )
		$attachment = one_audio_attachment( $mime_type, $file );

	elseif ( preg_match( '/text/', $mime_type ) )
		$attachment = one_text_attachment( $mime_type, $file );

	elseif ( preg_match( '/application/', $mime_type ) )
		$attachment = one_application_attachment( $mime_type, $file );

	echo apply_filters( 'one_attachment', $attachment );
}

/**
 * Handles application attachments on their attachment pages
 * Uses the <object> tag to embed media on those pages
 *
 * @todo Run a battery of tests on many different applications
 * @todo Figure out what to do with FLV files outside of the current functionality
 *
 * @since 0.3
 * @param string $mime attachment mime type
 * @param string $file attachment file URL
 * @return string
 */
function one_application_attachment( $mime = false, $file = false ) {
	$application = '<object class="text" type="' . $mime . '" data="' . $file . '" width="400">';
	$application .= '<param name="src" value="' . $file . '" />';
	$application .= '</object>';
	return apply_filters( 'one_application_attachment', $application );
}

/**
 * Handles text attachments on their attachment pages
 * Uses the <object> element to embed media in the pages
 *
 * @since 0.3
 * @param string $mime attachment mime type
 * @param string $file attachment file URL
 * @return string
 */
function one_text_attachment( $mime = false, $file = false ) {
	$text = '<object class="text" type="' . $mime . '" data="' . $file . '" width="400">';
	$text .= '<param name="src" value="' . $file . '" />';
	$text .= '</object>';
	return apply_filters( 'one_text_attachment', $text );
}

/**
 * Handles audio attachments on their attachment pages
 * Puts audio/mpeg and audio/wma files into an <object> element
 *
 * @todo Test out and support more audio types
 *
 * @since 0.2.2
 * @param string $mime attachment mime type
 * @param string $file attachment file URL
 * @return string
 */
function one_audio_attachment( $mime = false, $file = false ) {

	if ( $mime == 'audio/mpeg' || $mime == 'audio/wma' ) :

		$audio = '<object type="' . $mime . '" class="player audio" data="' . $file . '" width="400" height="50">';
			$audio .= '<param name="src" value="' . $file . '" />';
			$audio .= '<param name="autostart" value="false" />';
			$audio .= '<param name="controller" value="true" />';
		$audio .= '</object>';

	endif;

	return apply_filters( 'one_audio_attachment', $audio );
}

/**
 * Handles video attachments on attachment pages
 * Also handles application/octet-stream attachments if video (flv)
 * FLV files are loaded with Flowplayer (see one_enqueue_script() for JS)
 * Add other video types to the <object> element
 *
 * @todo Test out and support more video types
 *
 * @since 0.2.2
 * @param string $mime attachment mime type
 * @param string $file attachment file URL
 * @return string
 */
function one_video_attachment( $mime = false, $file = false ) {

	if ( !$mime || !$file ) :
		return;

	elseif ( $mime == 'video/asf' ) :

		$video = '<object type="video/x-ms-wmv" class="player video" data="' . $file . '" width="400" height="320">';
			$video .= '<param name="src" value="' . $file . '" />';
			$video .= '<param name="autostart" value="false" />';
			$video .= '<param name="allowfullscreen" value="true" />';
			$video .= '<param name="controller" value="true" />';
		$video .= '</object>';

	elseif ( $mime == 'video/quicktime' ) :

		$video = '<object type="video/quicktime" class="player video" data="' . $file . '">';
			$video .= '<param name="src" value="' . $file . '" />';
			$video .= '<param name="autoplay" value="false" />';
			$video .= '<param name="allowfullscreen" value="true" />';
			$video .= '<param name="controller" value="true" />';
		$video .= '</object>';

	elseif ( $mime == 'application/octet-stream' && preg_match( '/\.flv$/', $file ) ) :

		$video = '<div id="flash-video" class="video"></div>';
		$video .= '<script type="text/javascript">';
		$video .= 'flashembed("flash-video", "' . one_SWF . '/FlowPlayerDark.swf", {';
		$video .= 'config: { ';
			$video .= "videoFile: '" . $file . "', ";
			$video .= "initialScale: 'scale',";
			$video .= 'autoPlay: false,';
			$video .= 'loop: false,';
			$video .= 'showVolumeSlider: false,';
			$video .= "controlsOverVideo: 'ease',";
			$video .= 'controlBarBackgroundColor: -1,';
			$video .= "controlBarGloss: 'low'";
			$video .= '}';
		$video .= '});';
		$video .= '</script>';

	endif;

	return apply_filters( 'one_video_attachment', $video );
}

?>